package com.ghostwan.androears.adapter;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Set;

import android.content.Context;
import android.net.Uri;
import android.os.RemoteException;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;
import android.widget.Toast;

import com.ghostwan.androears.AndroEars;
import com.ghostwan.androears.R;
import com.ghostwan.androears.customObject.Album;
import com.ghostwan.androears.customObject.Artist;
import com.ghostwan.androears.customObject.Song;

public class SongAdapter extends BaseAdapter{
	private final LayoutInflater mInflater;
	private final int mLayoutRes;
	private ArrayList<Song> mSongList;
	public AndroEars mAndroEars;
	protected String TAG = "SongAdapter";
	
	public SongAdapter(AndroEars androEars, int textViewResourceId) {
		super();
		mAndroEars= androEars;
		mInflater = (LayoutInflater)mAndroEars.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mLayoutRes = textViewResourceId;
		mSongList = new ArrayList<Song>();
		// TODO Auto-generated constructor stub
	}
	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		View view;
		if( convertView == null)
		{
			view = mInflater.inflate(mLayoutRes, parent, false);
		}
		else
		{
			view = convertView;
		}
		Song song = this.getItem(position);
		LinearLayout addLayout = (LinearLayout) view.findViewById(R.id.addLayout);
		addLayout.setClickable(true);
		addLayout.setTag(new Integer(position));
		addLayout.setBackgroundResource(android.R.drawable.menuitem_background);
		addLayout.setOnClickListener(new OnClickListener()
		{
			
			@Override
			public void onClick(View v)
			{
				try
				{
					Song song = mSongList.get((Integer) v.getTag());
					mAndroEars.mPlayerService.addSongPlaylist(song);
					mAndroEars.mMusicPlayerButton.setVisibility(View.VISIBLE);
					String s = new String();
					s =  mAndroEars.getString(R.string.adding)+" "+song.mSongName+" "
					+mAndroEars.getString(R.string.adding_song_cp);
					Toast.makeText(mAndroEars,s,Toast.LENGTH_LONG).show();
				}
				catch (RemoteException e)
				{
					Log.e(TAG, e.getMessage());
				}
				
			}
		});
		TextView songName = (TextView)view.findViewById(R.id.songName);
		TextView songDuration = (TextView)view.findViewById(R.id.songDuration);
		songName.setText(song.mSongName);
		long z = song.mDuration;
		long s = (z/1000)%60;
		long m = (z/60000);
		NumberFormat formatter = new DecimalFormat("00");
		songDuration.setText(formatter.format(m)+":"+formatter.format(s));
		ImageView songCover = (ImageView)view.findViewById(R.id.songCover);
		
		if(song.mSongCover != null)
		{
	        songCover.setImageURI(Uri.parse(song.mSongCover));		
		}
		else
		{
			songCover.setImageResource(R.drawable.song_default_black);
		}
			
		
		// TODO Auto-generated method stub
		return view;
	}


	public void setSongList(ArrayList<Song> list)
	{
		mSongList = list;
		this.notifyDataSetChanged();
	}
	@Override
	public int getCount()
	{
		return mSongList.size();
	}


	@Override
	public Song getItem(int position)
	{
		return mSongList.get(position);
	}


	@Override
	public long getItemId(int position)
	{
		return position;
	}
	
}
